home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / test / locale / old / hello.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-10  |  2.4 KB  |  106 lines

  1. ;/* Sample use of CatComp
  2.  
  3. CatComp helloworld.cd cfile helloworld_strings.h objfile helloworld_strings.o
  4. Quit
  5. */
  6.  
  7. #include <exec/types.h>
  8. #include <libraries/locale.h>
  9. #include <stdio.h>
  10.  
  11. #include <clib/exec_protos.h>
  12. #include <clib/locale_protos.h>
  13.  
  14. #include <pragmas/exec_pragmas.h>
  15. #include <pragmas/locale_pragmas.h>
  16.  
  17. #define CATCOMP_STRINGS        // for demonstrating the reverse index functions
  18.                 // in the real program, the strings would appear
  19.                 // in this program;  I'm using the defines
  20.                 // for convenience
  21.  
  22. #include "helloworld_strings.h"
  23.  
  24.  
  25. extern struct Library *SysBase;
  26. struct Library *LocaleBase;
  27.  
  28.  
  29. #define Prototype extern
  30.  
  31. Prototype STRPTR GetString(LONG stringNum);
  32. Prototype LONG GetIndex(STRPTR string);
  33. Prototype void openCatalog(char *name);
  34. Prototype void closeCatalog(void);
  35.  
  36. APTR Catalog = NULL;
  37. char builtin[100][80]={NULL};
  38.  
  39. // fool locale.library into opening the english catalog
  40. static struct TagItem tags[3] = {
  41.     OC_Language,"english",
  42.     OC_BuiltInLanguage,"none",
  43.     TAG_DONE, NULL};
  44.  
  45. VOID main(VOID)
  46. {
  47. int i;
  48. char *string;
  49.  
  50.     LocaleBase = OpenLibrary("locale.library",38);
  51.  
  52.     // built translation table
  53.     if(LocaleBase) {
  54.     APTR catalog;
  55.         catalog = OpenCatalogA(NULL,"helloworld.catalog",tags);
  56.         for(i=0; i<100; i++) {    // build string table
  57.         if( *(string = GetCatalogStr(catalog,i,"")) == NULL)break;
  58.         strcpy(builtin[i],string);
  59.         }
  60.     CloseCatalog(catalog);
  61.     }
  62.     openCatalog("helloworld.catalog");
  63.     printf("Cat Index of [%s] is %d\n",MSG_BYE_STR,GetIndex(MSG_BYE_STR));
  64.     printf("Cat Index of [%s] is %d\n",MSG_HELLO_STR,GetIndex(MSG_HELLO_STR));
  65.  
  66.     printf("%s\n",GetString(GetIndex(MSG_HELLO_STR)));
  67.  
  68.     closeCatalog();
  69.     if (LocaleBase)CloseLibrary(LocaleBase);
  70. }
  71.  
  72. STRPTR GetString(LONG stringNum)
  73. {
  74.  
  75.     if (LocaleBase)
  76.         return(GetCatalogStr(Catalog,stringNum,builtin[stringNum]));
  77.  
  78.     return(builtin[stringNum]);
  79. }
  80.  
  81.  
  82. /* Given a string, scan through the local strings and find the index number
  83.  * which is used to access locale.library to get the string in the
  84.  * current catalog
  85.  */
  86.  
  87. LONG GetIndex(STRPTR string)
  88. {
  89. int i;
  90.  
  91.     for(i=0; i<100; i++) {
  92.     if(*builtin[i] == NULL)break;    // end of table
  93.     if(!Stricmp(builtin[i],string))return i;
  94.     }
  95.     return NULL;
  96. }
  97.  
  98. void openCatalog(char *name)
  99. {
  100.    if(LocaleBase)Catalog  = OpenCatalogA(NULL,name,NULL);
  101. }
  102.  
  103. void closeCatalog(void)
  104. {
  105.    if(LocaleBase && Catalog)CloseCatalog(Catalog);
  106. }